package com.xm.adv.activity;

import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.example.yf_a64_api.YF_A64_API_Manager;
import com.xm.adv.R;
import com.xm.adv.bean.OpenCloseBean;
import com.xm.adv.view.dialog.RoateDialog;
import com.xm.adv.view.dialog.VoiceDialog;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import xinya.com.baselibrary.baseactivity.ZjbBaseActivity;
import xinya.com.baselibrary.constant.Constant;
import xinya.com.baselibrary.eventbus.EventBusMsg;
import xinya.com.baselibrary.router.RouterUrl;
import xinya.com.baselibrary.router.RouterUtil;
import xinya.com.baselibrary.utils.ACacheX;
import xinya.com.baselibrary.utils.gson.GenericType;
import xinya.com.baselibrary.utils.gson.GsonUtils;
import xinya.com.baselibrary.view.dialog.EditDialog;
import xinya.com.baselibrary.view.dialog.TwoBtnDialog;

@Route(path = RouterUrl.Set.set_set)
public class SetActivity extends ZjbBaseActivity {

    @BindView(R.id.imageBack)
    ImageView imageBack;
    @BindView(R.id.textViewTitle)
    TextView textViewTitle;
    @BindView(R.id.textViewRight)
    TextView textViewRight;
    @BindView(R.id.line)
    TextView line;
    @BindView(R.id.viewBar)
    LinearLayout viewBar;
    @BindView(R.id.textName)
    TextView textName;
    @BindView(R.id.viewName)
    LinearLayout viewName;
    @BindView(R.id.textStartTime)
    TextView textStartTime;
    @BindView(R.id.viewStartTime)
    LinearLayout viewStartTime;
    @BindView(R.id.textVoice)
    TextView textVoice;
    @BindView(R.id.viewVoice)
    LinearLayout viewVoice;
    @BindView(R.id.textStart)
    TextView textStart;
    @BindView(R.id.switchStart)
    Switch switchStart;
    @BindView(R.id.textProtect)
    TextView textProtect;
    @BindView(R.id.switchProtect)
    Switch switchProtect;
    @BindView(R.id.viewStartSet)
    LinearLayout viewStartSet;
    @BindView(R.id.textLink)
    TextView textLink;
    @BindView(R.id.switchLink)
    Switch switchLink;
    @BindView(R.id.viewPsw)
    LinearLayout viewPsw;
    @BindView(R.id.viewRoate)
    LinearLayout viewRoate;
    @BindView(R.id.btnReSet)
    Button btnReSet;
    private String closeOpenTime;
    private int maxVoice;
    private int currentVoice;
    private VoiceDialog voiceDialog;
    private String openCloseEnable;
    private EditDialog editDialog;
    private RoateDialog roateDialog;
    private TwoBtnDialog twoBtnDialog;
    private YF_A64_API_Manager yfapi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set);
        ButterKnife.bind(this);
        setShouldReceiveEvent(true);
        init();
    }

    @Override
    protected void initSP() {
        yfapi = new YF_A64_API_Manager(this);
    }

    @Override
    protected void initIntent() {

    }

    @Override
    protected void initViews() {
        textViewTitle.setText("应用设置");
        initOpenCloseTime();
        AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        maxVoice = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        currentVoice = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        textVoice.setText("当前音量：" + currentVoice + "\n最大音量：" + maxVoice);
        openCloseEnable = ACacheX.getAsString(mContext, Constant.AcacheKey.setInfo, Constant.AcacheValue.openCloseEnable);
        if (TextUtils.isEmpty(openCloseEnable)){
            switchStart.setChecked(true);
            textStart.setText("开机自启：开");
        }else {
            switchStart.setChecked(TextUtils.equals("true", openCloseEnable));
            textStart.setText("开机自启："+(TextUtils.equals("true",openCloseEnable)?"开":"关"));
        }
        switchProtect.setChecked(Constant.keepLive);
        switchLink.setChecked(Constant.link);
        textProtect.setText(Constant.keepLive?"应用守护：开":"应用守护：关");
        setLink();
    }

    private void setLink() {
        viewStartTime.setEnabled(Constant.link);
        viewStartTime.setAlpha(Constant.link ? 1f : 0.6f);
        viewRoate.setEnabled(Constant.link);
        viewRoate.setAlpha(Constant.link ? 1f : 0.6f);
        textLink.setText(Constant.link?"联机模式":"单机模式");
    }

    /**
     * 显示开关机时间
     */
    private void initOpenCloseTime() {
        closeOpenTime = ACacheX.getAsString(mContext, Constant.AcacheKey.setInfo, Constant.AcacheValue.closeOpenTime);
        if (TextUtils.isEmpty(closeOpenTime)) {
            textStartTime.setText("开机时间：00:00\n关机时间：00:00");
        } else {
            try {
                List<OpenCloseBean> httpResult = GsonUtils.parseToGenericObject(closeOpenTime, new GenericType<List<OpenCloseBean>>() {
                });
                textStartTime.setText("开机时间：" + httpResult.get(0).getStartTime() + "\n关机时间：" + httpResult.get(0).getEndTime());
            } catch (Exception e) {
                textStartTime.setText("开机时间：00:00\n关机时间：00:00");
            }
        }
    }

    @Override
    protected void initRecyclerview() {

    }

    @Override
    protected void initListener() {
        switchStart.setOnCheckedChangeListener((buttonView, isChecked) -> {
            ACacheX.putAsString(mContext, Constant.AcacheKey.setInfo, Constant.AcacheValue.openCloseEnable, String.valueOf(isChecked));
            textStart.setText("开机自启："+(isChecked?"开":"关"));
        });
        switchProtect.setOnCheckedChangeListener((buttonView, isChecked) -> {
            ACacheX.putAsString(mContext, Constant.AcacheKey.setInfo, Constant.AcacheValue.keepLive, String.valueOf(isChecked));
            Constant.keepLive = isChecked;
            textProtect.setText(Constant.keepLive?"应用守护：开":"应用守护：关");

        });
        switchLink.setOnCheckedChangeListener((buttonView, isChecked) -> {
            ACacheX.putAsString(mContext, Constant.AcacheKey.setInfo, Constant.AcacheValue.link, String.valueOf(isChecked));
            Constant.link = isChecked;
            setLink();
        });
    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.imageBack, R.id.viewName, R.id.viewStartTime, R.id.viewVoice, R.id.viewPsw, R.id.viewRoate, R.id.btnReSet})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.imageBack:
                onBackPressed();
                break;
            case R.id.viewName:
                new EditDialog(mContext, "终端名称", "请输入终端名称", "确定", "取消", new EditDialog.ClickListenerInterface() {
                    @Override
                    public void doConfirm(String intro) {

                    }

                    @Override
                    public void doCancel() {

                    }
                }).show();
                break;
            case R.id.viewStartTime:
                RouterUtil.getPostcardWithTransition(mContext, RouterUrl.Set.set_openClose)
                        .navigation();
                break;
            case R.id.viewVoice:
                if (voiceDialog != null) {
                    voiceDialog.dismiss();
                    voiceDialog = null;
                }
                voiceDialog = new VoiceDialog(mContext, new VoiceDialog.OnVoiceChangeListener() {
                    @Override
                    public void change(int voice) {
                        textVoice.setText("当前音量：" + voice + "\n最大音量：" + maxVoice);
                    }
                });
                voiceDialog.show();
                break;
            case R.id.viewPsw:
                if (editDialog!=null){
                    editDialog.dismiss();
                    editDialog=null;
                }
                editDialog = new EditDialog(mContext, "密码设置", "请输入密码", "确认", "取消", new EditDialog.ClickListenerInterface() {
                    @Override
                    public void doConfirm(String intro) {

                    }

                    @Override
                    public void doCancel() {

                    }
                });
                editDialog.show();
                break;
            case R.id.viewRoate:
                if (roateDialog!=null){
                    roateDialog.dismiss();
                    roateDialog=null;
                }
                roateDialog = new RoateDialog(mContext, (screen1, screen2) -> {

                });
                roateDialog.show();
                break;
            case R.id.btnReSet:
                if (twoBtnDialog!=null){
                    twoBtnDialog.dismiss();
                    twoBtnDialog=null;                }
                twoBtnDialog = new TwoBtnDialog(mContext, "确定要初始化吗？", "确定", "取消", new TwoBtnDialog.ClickListenerInterface() {
                    @Override
                    public void doConfirm() {
                        ACacheX.clear(mContext,Constant.AcacheKey.setInfo);
                        Constant.keepLive=true;
                        Constant.link=true;
                        initViews();
                    }

                    @Override
                    public void doCancel() {

                    }
                });
                twoBtnDialog.show();
                break;
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventReceiver(EventBusMsg eventBusMsg) {
        switch (eventBusMsg.key) {
            case Constant.EventKey.setCloseOpenTime:
                initOpenCloseTime();
                break;
            default:
                break;
        }
    }
}
